package org.djw.org.service.impl;

import org.djw.basic.service.impl.BaseServiceImpl;
import org.djw.basic.util.MD5Utils;
import org.djw.basic.util.PageList;
import org.djw.basic.util.StrUtils;
import org.djw.org.domain.Employee;
import org.djw.org.mapper.EmployeeMapper;
import org.djw.org.query.EmployeeQuery;
import org.djw.org.service.IEmployeeService;
import org.djw.user.domain.Logininfo;
import org.djw.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 员工模块业务层
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void add(Employee employee) {
        //初始化employee
        initEmployee(employee);
        //拷贝一个logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }

    /**
     * 初始化employee
     * @param employee
     */
    private void initEmployee(Employee employee) {
        //获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        //使用盐值加密
        String mdPwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(mdPwd);
    }

    /**
     * 获取logininfo对象
     * @param employee
     * @return
     */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        //后台员工
        logininfo.setType(0);
        return logininfo;
    }
}
